package com.example.swd1.gudong.Tool;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.example.swd1.gudong.constant.Constant;

import java.util.Date;

/**
 * Created by user on 8/31/16.
 */
public class DatabaseHelper extends SQLiteOpenHelper {

    /*-begin-201609012-shile.wu-add-for-task2781345-*/
    private static DatabaseHelper helperInstance = null;

    public synchronized static DatabaseHelper getInstance(Context context) {
        if (helperInstance == null) {
            helperInstance = new DatabaseHelper(context);
        }
        return helperInstance;
    }
    /*-end-20160907-shile.wu-add-for-task2781345-*/


    public DatabaseHelper(Context context) {
        super(context, Constant.DATABASE_NAME, null, 1);
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        //Create table
        String sql = "create table  "
                + Constant.DATABASE_TABLE_NAME_SPORT_RECORD + "("
                + Constant.DATABASE_TABLE_FIELD_ID + "  integer primary key autoincrement,"
                + Constant.DATABASE_TABLE_FIELD_TIME + "  long,"
                + Constant.DATABASE_TABLE_FIELD_DISTANCE + "  float,"
                + Constant.DATABASE_TABLE_FIELD_AVG_SPEED + "  float,"
                + Constant.DATABASE_TABLE_FIELD_HOURS + " long,"
                + Constant.DATABASE_TABLE_FIELD_AVG_PACE + "  float )";
        db.execSQL(sql);

        fillDatabase(db);

        /*-begin-201609012-shile.wu-add-for-task2781345-*/
        sql = "create table  "
                + Constant.DATABASE_TABLE_NAME_MESSAGE_LIST + "("
                + Constant.DATABASE_TABLE_FIELD_MESSAGE_ID + "  integer primary key autoincrement,"
                + Constant.DATABASE_TABLE_FIELD_MESSAGE_TITLE + "  text,"
                + Constant.DATABASE_TABLE_FIELD_MESSAGE_TIME + "  text,"
                + Constant.DATABASE_TABLE_FIELD_MESSAGE_CONTENT + " text,"
                + Constant.DATABASE_TABLE_FIELD_MESSAGE_UNREADNUM + "  integer )";
        db.execSQL(sql);
        /*-end-20160907-shile.wu-add-for-task2781345-*/

    }

    private void fillDatabase(SQLiteDatabase db) {
        ContentValues values;
        for (int i = 0; i < 8; i++) {
            values = new ContentValues();
            values.put(Constant.DATABASE_TABLE_FIELD_TIME, new Date(2016, i+1, (int) (30 * Math.sin(i)), 13, 44, 2).getTime());
            values.put(Constant.DATABASE_TABLE_FIELD_DISTANCE, 13.02 + i);
            values.put(Constant.DATABASE_TABLE_FIELD_AVG_SPEED, 20 + i);
            values.put(Constant.DATABASE_TABLE_FIELD_HOURS, 30 + 90 * i);
            values.put(Constant.DATABASE_TABLE_FIELD_AVG_PACE, 110 + 73 * i);

            db.insert(Constant.DATABASE_TABLE_NAME_SPORT_RECORD, null, values);
        }

        values = new ContentValues();
        values.put(Constant.DATABASE_TABLE_FIELD_TIME, new Date(2015, 1,1, 13, 44, 2).getTime());
        values.put(Constant.DATABASE_TABLE_FIELD_DISTANCE, 13.02);
        values.put(Constant.DATABASE_TABLE_FIELD_AVG_SPEED, 20);
        values.put(Constant.DATABASE_TABLE_FIELD_HOURS, 30 + 90 );
        values.put(Constant.DATABASE_TABLE_FIELD_AVG_PACE, 110 + 73 );
        db.insert(Constant.DATABASE_TABLE_NAME_SPORT_RECORD, null, values);
        values = new ContentValues();
        values.put(Constant.DATABASE_TABLE_FIELD_TIME, new Date(2015, 1,1, 13, 44, 2).getTime());
        values.put(Constant.DATABASE_TABLE_FIELD_DISTANCE, 14.02);
        values.put(Constant.DATABASE_TABLE_FIELD_AVG_SPEED, 20);
        values.put(Constant.DATABASE_TABLE_FIELD_HOURS, 30 + 90 );
        values.put(Constant.DATABASE_TABLE_FIELD_AVG_PACE, 110 + 73 );
        db.insert(Constant.DATABASE_TABLE_NAME_SPORT_RECORD, null, values);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}
